Road Network

Author

Juan Fonseca

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dodgr)
library(osmextract)
Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright.
Check the package website, https://docs.ropensci.org/osmextract/, for more details.
library(sf)
Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(tmap)
Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
remotes::install_github('r-tmap/tmap')

The following code uses some functions compiled in the MinorRoadTraffic repository prepared by Malcolm Morgan and follows the process described in this vignette of the same package.

# remotes::install_github("ITSLeeds/MinorRoadTraffic") # if not installed
library(MinorRoadTraffic)

Downloading data

osm_raw <- oe_get("East Yorkshire with Hull",
                 extra_tags = c("ref", "highway", "junction", "maxspeed"))
The input place was matched with: East Yorkshire with Hull
The chosen file was already detected in the download directory. Skip downloading.
The corresponding gpkg file was already detected. Skip vectortranslate operations.
Reading layer `lines' from data source 
  `C:\Users\ts18jpf\Documents\OSMEXT_downloads\geofabrik_east-yorkshire-with-hull-latest.gpkg' 
  using driver `GPKG'
Simple feature collection with 77201 features and 12 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: -4.03885 ymin: 51.265 xmax: 4.364931 ymax: 58.67265
Geodetic CRS:  WGS 84

Clipping road network

The count data will be used to clip the road network.

sf_counts <- st_read("03_preprocessing_files/grouped_counts.geojson")
Reading layer `grouped_counts' from data source 
  `C:\Users\ts18jpf\OneDrive - University of Leeds\02_MsC\99_GEOG5099M_Dissertation\GEOG5099_Analysis\03_preprocessing_files\grouped_counts.geojson' 
  using driver `GeoJSON'
Simple feature collection with 588 features and 4 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 505930.6 ymin: 428325.3 xmax: 513352.9 ymax: 433636.7
Projected CRS: OSGB36 / British National Grid
buffer_distance = max(st_distance(sf_counts,sf_counts))*0.15

bounds <- sf_counts |>
  make_convex_hull(dist = buffer_distance)

osm_clean = extract_from_osm(osm_raw, bounds)

network = osm_clean$network
junctions = osm_clean$junctions
rm(osm_clean, osm_raw)
tmap_mode("view")
tmap mode set to interactive viewing
tm_basemap()+
tm_shape(bounds)+
  tm_polygons(alpha = 0.3)+
  tm_shape(network)+
  tm_lines(col = "orange",alpha = 0.4)+
  tm_shape(sf_counts)+
  tm_dots(col = "blue")

Cleaning and simplifying

The ref attribute is filled based on the neighbouring links.

source("osm_fill_ref2.R")
network_fill2 = osm_fill_ref2(network)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

Spatial join to count data